package com.leorui.schoollife.utils

import com.leorui.schoollife.entity.ContentEntity
import com.leorui.schoollife.entity.SchoolAnnouncementDetailEntity
import com.leorui.schoollife.entity.SchoolAnnouncementEntity
import org.jsoup.Jsoup
import org.jsoup.select.Elements
import java.util.*

object SchoolParseUtils {
    fun parseHomePage(type: Int, html: String, baseUrl: String): List<SchoolAnnouncementEntity?>? {

        // 解析主页
        when (type) {
            0 -> {
                // 通用类型
                return null
            }
            1 -> {
                // 特定类型
                return this.parseHomePageType1(html, baseUrl)
            }
        }
        return null
    }

    fun parseDetailPage(type: Int, html: String, baseUrl: String): SchoolAnnouncementDetailEntity? {
        // 解析非主页
        when (type) {
            0 -> {
                // 通用类型
                return null
            }
            1 -> {
                return this.parseContentType1(html, baseUrl.substring(0, baseUrl.length - 1))
            }
        }
        return null
    }

    private fun parseHomePageType1(html: String, baseUrl: String): List<SchoolAnnouncementEntity?>? {
        if (html.isEmpty()) {
            return null
        }
        val announcementList = LinkedList<SchoolAnnouncementEntity>()
        val document = Jsoup.parse(html)
        val titbarElement: Elements? = document.getElementsByClass("titbar")
        titbarElement?.forEach { titbar ->
            if (titbar.childrenSize() > 0) {
                val title: String? = titbar.child(0).text()
                val brotherElements: Elements? = titbar.nextElementSibling()?.children()
                val contentList = LinkedList<ContentEntity>()
                brotherElements?.forEach { brother ->
                    if (brother.tagName() == "li") {
                        val content = ContentEntity()
                        brother.children().forEachIndexed { index, element ->
                            if (index % 2 == 0) {
                                val time = element.text()
                                content.time = time
                            } else {
                                val contentMsg = element.text()
                                val url = element.attr("href")
                                content.content = contentMsg
                                content.url = baseUrl + url
                            }
                        }
                        contentList.add(content)
                    }
                }
                announcementList.add(SchoolAnnouncementEntity(title, contentList))
            }

        }
        return announcementList
    }

    private fun parseContentType1(html: String, baseUrl: String): SchoolAnnouncementDetailEntity? {

        if (html.isEmpty()) {
            return null
        }
        val detailEntity = SchoolAnnouncementDetailEntity()
        detailEntity.contentList = LinkedList()
        detailEntity.imgList = LinkedList()
        val document = Jsoup.parse(html)
        val body = document.getElementById("inner")
        val form = body?.getElementsByTag("form")
        if (form?.size == 0) {
            return null
        }

        val tBody = form?.get(0)?.getElementsByTag("tbody")

        if (tBody?.size == 0) {
            return null
        }
        if (tBody?.get(0)?.childrenSize() == 0) {
            return null
        }
        val trs = tBody?.get(0)?.children()
        if (trs?.size ?: 0 < 3) {
            return null
        }
        val titleTr = trs?.get(0)
        val timeAndAuthorTr = trs?.get(1)
        val contentTr = trs?.get(3)

        val titlestyle122328Elements = titleTr?.getElementsByClass("titlestyle122328")

        for (index in 0 until (titlestyle122328Elements?.size ?: 0)) {
            if (index == 0) {
                detailEntity.title = titlestyle122328Elements?.get(0)?.text()
            }
        }
        val timestyle122328Elements = timeAndAuthorTr?.getElementsByClass("timestyle122328")
        for (index in 0 until (timestyle122328Elements?.size ?: 0)) {
            if (index == 0) {
                detailEntity.time = timestyle122328Elements?.get(0)?.text()
            }
        }
        val authorstyle122328Elements = timeAndAuthorTr?.getElementsByClass("authorstyle122328")
        for (index in 0 until (authorstyle122328Elements?.size ?: 0)) {
            if (index == 0) {
                detailEntity.author = authorstyle122328Elements?.get(0)?.text()
            }
        }

        val vNewsContents = contentTr?.getElementsByClass("v_news_content")
        if (vNewsContents?.size == 0) {
            return null
        }
        val newContent = vNewsContents?.get(0)
        newContent?.children()?.forEach {
            for (index in 0 until it.childrenSize()) {
                if (index == 0) {
                    if (it.child(0).tagName() == "span") {
                        detailEntity.contentList?.add(it.child(0).text())
                    } else if (it.child(0).tagName() == "img") {
                        detailEntity.contentList?.add(baseUrl + it.child(0).attr("src"))
                    }
                }
            }
        }


        return detailEntity
    }
}